package com.ziq.zcamp.place

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter
import com.qweather.sdk.bean.weather.WeatherDailyBean
import com.ziq.base.extension.click
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.activities.ActivitiesInfo
import com.ziq.zcamp.bean.coupon.CouponCampOnline
import com.ziq.zcamp.bean.place.DetailGroup
import com.ziq.zcamp.bean.place.PlaceDetail
import com.ziq.zcamp.bean.place.PlaceScoreInfo
import com.ziq.zcamp.bean.post.PostingList
import com.ziq.zcamp.bean.product.PlaceProduct
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.post.PostingAdapter

class PlaceDetailAdapter(val activity: PlaceDetailActivity, val screenWidth: Int, val postingItemWidth:Int = 0) : GroupedRecyclerViewAdapter(activity){

    companion object {
        const val TYPE_HEADER_ACT = 12 //活动
        const val TYPE_CHILD_ACT = 13

        const val TYPE_HEADER_BOOKING = 1 //预订
        const val TYPE_CHILD_BOOKING = 2
        const val TYPE_FOOTER_BOOKING = 3

        const val TYPE_CHILD_APPRAISE = 5 //订单评价帖子

        const val TYPE_CHILD_PARAMETERS = 8 //营地信息参数

        const val TYPE_HEADER_INTRODUCE = 9 //营地介绍
        const val TYPE_CHILD_INTRODUCE = 10
        const val TYPE_FOOTER_INTRODUCE = 11

    }

    var groupList: MutableList<DetailGroup> = mutableListOf()
    var introduceImageWidth = 0

    var actGroup = PlaceDetailActGroup(activity, this)
    var bookingGroup = PlaceDetailBookingGroup(this)

    var appriseGroup = PlaceDetailAppraiseGroup(this)

    var parametersGroup = PlaceParametersDetailGroup(activity,this)
    var introduceGroup = PlaceIntroduceDetailGroup(activity,this)


    init {
        groupList.add(parametersGroup)
        groupList.add(introduceGroup)
        introduceImageWidth = screenWidth - GlobalContextManager.getResources().getDimensionPixelSize(
            R.dimen.dimens_dip_48)
    }

    var placeProductActionListener: PlaceProductActionListener? = null
    var postingAdapterAction: PostingAdapter.PostingAdapterAction? = null
    var campId: Int? = null

    //活动
    @Synchronized fun setActData(activities : List<ActivitiesInfo>?) {
        if(activities?.isNotEmpty() == true){
            if(!groupList.contains(actGroup)){
                groupList.add(0, actGroup)
            }
            actGroup.activities = activities
        } else {
            if(groupList.contains(actGroup)){
                groupList.remove(actGroup)
            }
            actGroup.activities = null
        }
        notifyDataChanged()
    }

    //商品预订
    @Synchronized fun setProductData(list: MutableList<PlaceProduct>?, showLimit:Int, couponList: List<CouponCampOnline>?) {
        bookingGroup.couponList = couponList
        bookingGroup.moreLimit = showLimit
        if(list?.isNotEmpty() == true){
            if(!groupList.contains(bookingGroup)){
                if(groupList.contains(actGroup)){
                    groupList.add(1, bookingGroup)
                } else {
                    groupList.add(0, bookingGroup)
                }
            }
            bookingGroup.productList = list
        } else {
            if(groupList.contains(bookingGroup)){
                groupList.remove(bookingGroup)
            }
            bookingGroup.productList = null
        }
        notifyDataChanged()
    }

    //大众印象
    @Synchronized fun setAppraiseData(scoreInfo: PlaceScoreInfo?) {
        if(scoreInfo != null && (scoreInfo.list?.isNotEmpty() == true)){
            if(!groupList.contains(appriseGroup)){
                groupList.add(groupList.size - 2, appriseGroup)
            }
            appriseGroup.scoreInfo = scoreInfo
        } else {
            if(groupList.contains(appriseGroup) && appriseGroup.postingList == null){
                groupList.remove(appriseGroup)
            }
            appriseGroup.scoreInfo = null
        }
        notifyDataChanged()
    }

    //打卡
    @Synchronized fun setPostingData(postingList: PostingList?) {
        if(postingList?.list?.isNotEmpty() == true || (postingList?.count?:0) > 0){
            if(!groupList.contains(appriseGroup)){
                groupList.add(groupList.size - 2, appriseGroup)
            }
            appriseGroup.postingList = postingList
        } else {
            if(groupList.contains(appriseGroup) && appriseGroup.scoreInfo == null){
                groupList.remove(appriseGroup)
            }
            appriseGroup.postingList = null
        }
        notifyDataChanged()
    }

    fun hasActCard() :Boolean{
        return groupList.contains(actGroup)
    }

    fun hasBookingCard() :Boolean{
        return groupList.contains(bookingGroup)
    }

    fun hasAppraiseCard() :Boolean{
        return groupList.contains(appriseGroup)
    }

    //详情
    fun setData(placeDetail: PlaceDetail?) {
        parametersGroup.setData(placeDetail)
        introduceGroup.setData(placeDetail)
        showEmptyView(true)
        notifyDataChanged()
    }

    fun setWeatherData(weatherDailyBean : WeatherDailyBean.DailyBean?){
        parametersGroup.setWeatherData(weatherDailyBean)
        notifyDataChanged()
    }

    //预订header 位置
    fun getBookingGroupHeaderPosition():Int{
//        val index = groupList.indexOf(bookingGroup)
//        if(index >= 0){
//            return countGroupRangeItem(0, index)
//        }
        return 0
    }

    //打卡header 位置
    fun getAppraiseGroupHeaderPosition():Int{
        val index = groupList.indexOf(appriseGroup)
        if(index >= 0){
            return countGroupRangeItem(0, index)
        }
        return 0
    }

    //信息header 位置
    fun getParametersGroupHeaderPosition():Int{
        if(parametersGroup.placeDetail != null){
            val index = groupList.indexOf(parametersGroup)
            if(index >= 0){
                return countGroupRangeItem(0, index)
            }
        }
        return 0
    }

    //介绍header 位置
    fun getIntroduceGroupHeaderPosition():Int{
        if(introduceGroup.placeDetail != null){
            val index = groupList.indexOf(introduceGroup)
            if(index >= 0){
                return countGroupRangeItem(0, index)
            }
        }
        return 0
    }



    override fun getGroupCount(): Int {
        return groupList.size
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return groupList.get(groupPosition).getChildrenCount()
    }

//    override fun isEmptyPosition(position: Int): Boolean {
//        val index = groupList.indexOf(postingGroup)
//        if(index >= 0){
//            if(postingGroup.postingList?.size?:0 == 0 && position == countGroupRangeItem(0, index) + 1){
//                return true
//            }
//        }
//        return false
//    }

    override fun getEmptyView(parent: ViewGroup?): View? {
        val view = LayoutInflater.from(mContext).inflate(R.layout.layout_place_detail_posting_empty, parent, false)
//            val container = view.findViewById<View>(R.id.container)
//            val lp = container.layoutParams
//            // 48 + 40 = 88
//            lp.height = activity.binding.statusView.measuredHeight - GlobalContextManager.getResources().getDimensionPixelSize(R.dimen.dimens_dip_88)
//            container.layoutParams = lp
        view.findViewById<View>(R.id.btn_posting).click {
            parametersGroup.placeDetail?.let { it1 -> activity.publishPosting(it1) }
        }
        return view
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return groupList[groupPosition].hasHeader(groupPosition)
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return groupList[groupPosition].hasFooter(groupPosition)
    }

    override fun getHeaderViewType(groupPosition: Int): Int {
        return groupList[groupPosition].getHeaderViewType(groupPosition)
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        return groupList[groupPosition].getChildViewType(groupPosition, childPosition)
    }

    override fun getFooterViewType(groupPosition: Int): Int {
        return groupList[groupPosition].getFooterViewType(groupPosition)
    }

    override fun getHeaderLayout(viewType: Int): Int {
        return when (viewType) {
            TYPE_HEADER_ACT -> {
                R.layout.item_place_detail_act_header
            }
            TYPE_HEADER_BOOKING -> {
                R.layout.item_place_detail_booking_header
            }
            TYPE_HEADER_INTRODUCE -> {
                R.layout.item_place_detail_introduce_header
            }
            else -> {
                0
            }
        }
    }

    override fun getChildLayout(viewType: Int): Int {
        return when (viewType) {
            TYPE_CHILD_ACT -> {
                R.layout.item_place_detail_act
            }
            TYPE_CHILD_BOOKING -> {
                R.layout.item_place_detail_booking
            }
            TYPE_CHILD_APPRAISE -> {
                R.layout.item_place_detail_appraise
            }
            TYPE_CHILD_PARAMETERS -> {
                R.layout.item_place_detail_parameters
            }
            TYPE_CHILD_INTRODUCE -> {
                R.layout.item_place_detail_introduce
            }
            else -> {
                0
            }
        }
    }

    override fun getFooterLayout(viewType: Int): Int {
        return when (viewType) {
            TYPE_FOOTER_BOOKING -> {
                R.layout.item_place_detail_booking_footer
            }
            TYPE_FOOTER_INTRODUCE -> {
                R.layout.item_place_detail_introduce_footer
            }
            else -> {
                0
            }
        }
    }

    override fun onBindHeaderViewHolder(holder: com.donkingliang.groupedadapter.holder.BaseViewHolder?, groupPosition: Int) {
        groupList[groupPosition].onBindHeaderViewHolder(holder, groupPosition)
    }

    override fun onBindChildViewHolder(
        holder: com.donkingliang.groupedadapter.holder.BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {
        groupList[groupPosition].onBindChildViewHolder( holder, groupPosition, childPosition)
    }

    override fun onBindFooterViewHolder(holder: com.donkingliang.groupedadapter.holder.BaseViewHolder?, groupPosition: Int) {
        groupList[groupPosition].onBindFooterViewHolder(holder, groupPosition)
    }

}

